package com.yxjme.stateviewlayoutlibrary;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;



public class LoadingView extends FrameLayout {

    private ProgressBar progressBar ;

    private ImageView imageView ;

    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        progressBar=new ProgressBar(getContext());
        progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_round));
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = 100 ;
        layoutParams.height = 100 ;
        addView(progressBar,layoutParams);


        layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        imageView = new ImageView(getContext());
        layoutParams.width = 150 ;
        layoutParams.height = 150 ;
        addView(imageView,layoutParams);
        imageView.setVisibility(GONE);
    }



    /**
     * 使用gif加载动画
     *
     * @param id
     */
    public void setGifIconAnim(int id){
        imageView.setImageResource(id);
        imageView.setVisibility(VISIBLE);
        progressBar.setVisibility(GONE);
//        Glide.with(getContext())
//                .asGif()
//                .load(id)
//                .into(imageView);
        imageView.setImageResource(id);
    }




    /**
     * 设置大小
     *
     * @param w
     * @param h
     */
    public void setSize(float w ,float h){
        if (progressBar!=null){
            ViewGroup.LayoutParams p = progressBar.getLayoutParams();;
            p.width = (int) w;
            p.height = (int) h;
            progressBar.setLayoutParams(p);
        }
    }
}
